package com.shushan.home.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shushan.base.Constants;
import com.shushan.db.ChildBookDao;
import com.shushan.pages.bean.SubjectSyncExerciseBean;
import com.shushan.shushanbao.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName: RecommendLessonsAdapter
 * @Desciption: //推荐课程适配器
 * @author: zhangshihao
 * @date: 2018-09-20
 */
public class RecommendLessonsAdapter extends RecyclerView.Adapter<RecommendLessonsAdapter.ViewHolder>{

    private static final String TAG = RecommendLessonsAdapter.class.getSimpleName();

    private List<SubjectSyncExerciseBean> subjectBeans;
    private int[] imgIds = {R.drawable.ic_sw_recommend,R.drawable.ic_speaking_recommend,R.drawable.ic_ew_recommend,R.drawable.ic_hearing_recommend,R.drawable.ic_oc_recommend};
    private OnItemClickListener onItemClickListener;
    private Context mContext;
    private HashMap<Integer,Integer> coversMap = new HashMap<>();

    public RecommendLessonsAdapter(Context context,List<SubjectSyncExerciseBean> subjectBeans){
        this.subjectBeans = subjectBeans;
        mContext = context;

        coversMap.put(Constants.TYPE_STRANGE_WORDS_EXERCISE,imgIds[0]);
        coversMap.put(Constants.TYPE_SPEAKING_EXERCISE,imgIds[1]);
        coversMap.put(Constants.TYPE_ENGLISH_WORDS_EXERCISE,imgIds[2]);
        coversMap.put(Constants.TYPE_HEARING_EXERCISE,imgIds[3]);
        coversMap.put(Constants.TYPE_ORAL_CALCULATE_EXERCISE,imgIds[4]);

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recommend_lesson,null,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final SubjectSyncExerciseBean exerciseBean = subjectBeans.get(position);
        holder.ivCover.setImageResource(getCoverByType(exerciseBean.practiceType));
        holder.tvLessonName.setText(exerciseBean.outline_detail_name);
        holder.tvRewardTime.setText("奖励："+exerciseBean.reward);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null){
                    onItemClickListener.onItemClick(position,exerciseBean.practiceType);
                }
            }
        });
    }

    private int getCoverByType(int type){
        int resId = 0;
        for(int i : coversMap.keySet()){
            if(i == type){
                resId = coversMap.get(type);
            }
        }
        return resId;
    }

    @Override
    public int getItemCount() {
        return subjectBeans.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView ivCover;
        private TextView tvLessonName;
        private TextView tvRewardTime;
        public ViewHolder(View itemView) {
            super(itemView);
            ivCover = itemView.findViewById(R.id.iv_recommend_pic);
            tvLessonName = itemView.findViewById(R.id.tv_lesson_name);
            tvRewardTime = itemView.findViewById(R.id.tv_reward_time);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(int position,int practiceType);
    }

}
